package com.example.service.appointment;


import com.example.common.enums.ReservationStatusEnum;
import com.example.common.enums.RoleEnum;
import com.example.mapper.appointment.MedicalCardMapper;
import com.example.mapper.appointment.RecordMapper;
import com.example.model.entity.appointment.MedicalCard;
import com.example.model.entity.appointment.Record;
import com.example.model.entity.other.Account;
import com.example.model.vo.RecordVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * 就诊记录表业务处理
 **/
@Service
public class RecordService {

    @Resource
    private RecordMapper recordMapper;
    @Resource
    private MedicalCardMapper medicalCardMapper;

    /**
     * 新增
     */
    public void add(Record record) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && RoleEnum.DOCTOR.name().equals(currentUser.getRole())) {
            record.setDoctorId(currentUser.getId());
            recordMapper.insert(record);
            MedicalCard medicalCard = new MedicalCard();
            medicalCard.setRecordId(record.getId());
            medicalCardMapper.insert(medicalCard);
        }

    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        recordMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            recordMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Record record) {
        recordMapper.updateById(record);
    }

    /**
     * 根据ID查询
     */
    public Record selectById(Integer id) {
        return recordMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<RecordVO> selectAll(Record record) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.HOSPITAL.name().equals(currentUser.getRole())){
            record.setHospitalId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            record.setDoctorId(currentUser.getId());
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            record.setUserId(currentUser.getId());
        }
        return recordMapper.selectAll(record);
    }

    /**
     * 分页查询
     */
    public PageInfo<RecordVO> selectPage(Record record, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.HOSPITAL.name().equals(currentUser.getRole())){
            record.setHospitalId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            record.setDoctorId(currentUser.getId());
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            record.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<RecordVO> list = recordMapper.selectAll(record);
        return PageInfo.of(list);
    }

}
